package 栈;

import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/implement-queue-using-stacks/
 */
public class _232_用栈实现队列 {

    private Stack<Integer> inStack;
    private Stack<Integer> outStack;


    public _232_用栈实现队列() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }

    /**
     * 入队
     */
    public void push(int x) {
        inStack.push(x);
    }
    //出队 获取队头 其实操作都一样 都是获取头部元素
    /**
     * 出队
     */
    public int pop() {
        checkOutStack();
        return outStack.pop();
    }

    /**
     * 获取队头元素
     */
    public int peek() {
        checkOutStack();
        return outStack.peek();
    }

    /**
     * 判断两个栈为空  才为空
     */
    public boolean empty() {
        return inStack.isEmpty() && outStack.isEmpty();

    }

    private void checkOutStack() {
        if (outStack.isEmpty()) {
            while (!inStack.isEmpty())
                outStack.push(inStack.pop());
        }
    }
}
